<?php 

/**
 * 问题显示页面
 */

namespace Home\Controller;
use Hdphp\Library\Controller;

Class ShowController extends CommonController{

	public function index(){
        //提交回答
        if(IS_POST && !empty($_POST['content']) ){
            $content=htmlspecialchars($_POST['content']);
            $asid=intval($_POST['qid']);
            $time=time();

            //$askTitle[0]['asid']
            $inserSql="INSERT INTO hd_answer SET content='{$content}',time={$time},uid={$_SESSION['uid']},asid={$asid} ";

            //同时更新 问题表和用户表的回答问题字段
            $updateSql="UPDATE hd_ask as a,hd_user as u SET a.answer=a.answer+1, u.answer=u.answer+1 WHERE asid=$asid AND u.uid={$_SESSION['uid']}";

            if( M()->exec($inserSql) && M()->exec($updateSql) ){
                $this->success('回答完成',"./index.php?c=show&asid={$asid}");
            }else{
                $this->error('回答失败');
            }
            
        }

		//读取顶级分类
        $this->topNav();

		//显示提问的问题
        $asid=isset($_GET['asid']) ? intval($_GET['asid']) :0;
        $sql="SELECT * FROM hd_ask WHERE asid={$asid}";
        if( !M()->query($sql) ){
            $this->display();die; //安全提示过滤
        }

        $sql="SELECT * FROM hd_ask as a JOIN hd_user as u ON a.uid=u.uid WHERE asid={$asid}";
        $askTitle=M()->query($sql);
        $this->assign('askTitle',$askTitle);
// p($askTitle);die;

        //问题回答列表
		//查找问题表和提问表和用户表 asid uid相关联的内容提取并且把重复的字段重命名
		$sql="SELECT a.content as acontent,a.time as atime,s.content as scontent, s.time as stime,s.accept as saccept,u.accept as useraccept,u.*,a.*,s.* FROM hd_ask as a JOIN hd_answer as s JOIN hd_user as u ON a.asid=s.asid AND u.uid=a.uid WHERE a.asid={$asid}  ";
            //AND s.accept=0
            //采纳+用户数据+回答列表
		$answerData=M()->query($sql);
		$this->assign('answerData',$answerData);
// p($answerData);die;

		//总回答数
		$totalAnswer=count($answerData);
		$this->assign('totalAnswer',$totalAnswer);

		//满意回答
		$sql="SELECT a.accept as anaccept,u.accept as uaccept, a.*,u.* FROM hd_answer as a JOIN hd_user as u ON a.uid=u.uid WHERE a.accept=1 AND asid={$asid}";
		$accept=M()->query($sql);
		$this->assign('accept',$accept);
// p($accept);die;

        //待解决的相关问题   使用提问表中的cid 只显示待解决的问题
        if( isset($answerData[0]['cid']) && !$askTitle[0]['solve']){                 
              $sql="SELECT * FROM hd_ask WHERE cid={$answerData[0]['cid']} ORDER BY RAND() LIMIT 5";
              $relateAsk=M()->query($sql);
              $this->assign('relateAsk',$relateAsk);
              // p($relateAsk);die();
        }


        //通过asid 或者 cid 获取面包屑子id
		$sql="SELECT * FROM hd_ask WHERE asid={$asid}";
		$res= M()->query($sql);
		$cid= isset($_GET['cid']) ? intval($_GET['cid']) : $res[0]['cid'];

		//处理面包屑导航
        $this->Breadcrumbs($cid);

        //问答右部统计信息
        $this->rightInfo();

		$this->display();
	}

    //采纳
    public function accept(){
        $anid=isset($_GET['anid']) ? intval($_GET['anid']) : $this->error('非法操作');
        //更新 hd_answer采纳 hd_user采纳加1 hd_ask已解决
        $sql="UPDATE hd_answer as an, hd_user as u,hd_ask as a SET an.accept=an.accept+1,u.accept=u.accept+1,a.solve=1 WHERE an.uid=u.uid AND anid={$anid} AND an.asid=a.asid";
        M()->exec($sql);

        $this->error('采纳成功');
    }

/* 
满意回答

           [anaccept] => 0
            [uaccept] => 9
            [anid] => 25
            [content] => www.houdunwang.com
            [time] => 1378115896
            [accept] => 9
            [uid] => 2
            [asid] => 23
            [username] => houdunwang
            [passwd] => 7fef6171469e80d32c0559f88b377245
            [ask] => 2
            [answer] => 30
            [point] => 145
            [exp] => 127
            [restime] => 1377048366
            [logintime] => 1378115529
            [loginip] => 124.205.157.210
            [lock] => 0
            [face] => */


/* 

问题列表

   [0] => Array
        (
            [acontent] => 后盾网的论坛网址是什么？
            [atime] => 1378113501
            [scontent] => www.houdunwang.com
            [stime] => 1378115896
            [asid] => 23
            [content] => www.houdunwang.com
            [time] => 1378115896
            [solve] => 0
            [answer] => 2
            [reward] => 50
            [uid] => 2
            [cid] => 15
            [anid] => 25
            [accept] => 0
        )

    [1] => Array
        (
            [acontent] => 后盾网的论坛网址是什么？
            [atime] => 1378113501
            [scontent] => bbs.houdunwang.com
            [stime] => 1378115912
            [asid] => 23
            [content] => bbs.houdunwang.com
            [time] => 1378115912
            [solve] => 0
            [answer] => 2
            [reward] => 50
            [uid] => 2
            [cid] => 15
            [anid] => 26
            [accept] => 0
        )
	*/


}

 ?>